package com.yxw.yx_hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yx_hotel.dto.*;
import com.yxw.yx_hotel.entity.YxRoomBedEntity;
import com.yxw.yx_hotel.entity.YxRoomPropertyEntity;
import com.yxw.yx_hotel.entity.YxRoomRepairEntity;
import com.yxw.yx_hotel.entity.YxStayWaitEntity;
import com.yxw.yx_hotel.entity.YxStayInEntity;
import com.yxw.yx_hotel.mapper.RoomBedMapper;
import com.yxw.yx_hotel.mapper.RoomMapper;
import com.yxw.yx_hotel.mapper.RoomPropertyMapper;
import com.yxw.yx_hotel.mapper.RoomRepiarMapper;
import com.yxw.yx_hotel.mapper.StayWaitMapper;
import com.yxw.yx_hotel.mapper.StayInMapper;
import com.yxw.yx_hotel.service.RoomPropertyService;
import com.yxw.yx_hotel.service.RoomRepairService;
import com.yxw.yx_hotel.utils.PageUtils;
import com.yxw.yx_hotel.utils.UserUtil;
import com.yxw.yx_hotel.vo.RoomPropertyVo;
import com.yxw.yx_hotel.vo.RoomRepairVo;
import com.yxw.yx_hotel.vo.UserTokenVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  宿舍资产管理
 * @author liuxin
 * @date 2024.01.17
 */
@Service
public class RoomRepairServiceImpl extends ServiceImpl<RoomRepiarMapper, YxRoomRepairEntity> implements RoomRepairService {

    @Autowired
    private UserUtil  userUtil;
    
    @Autowired
    private RoomBedMapper  roomBedMapper;
    
    @Autowired
    private StayWaitMapper stayWaitMapper;
    
    @Autowired
    private StayInMapper stayInMapper;

    /**
     * 新增宿舍资产
     * @param dto
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addRoomRepair(AddRoomRepairDto dto) {
        //校验维修宿舍是否存在在住员工

        UserTokenVo userVo = userUtil.getLoginUserVo();
        if(dto.getRepairType() == 1)
        {   
            LambdaQueryWrapper<YxRoomBedEntity> bedWrapper=new LambdaQueryWrapper();
            bedWrapper.eq(YxRoomBedEntity::getRoomId,dto.getRoomId());
            bedWrapper.ne(YxRoomBedEntity::getStatus,2);
            bedWrapper.eq(YxRoomBedEntity::getTenantId,userVo.getTenantId());
            Integer bedCount = roomBedMapper.selectCount(bedWrapper);
            if(bedCount > 0){
                throw new BaseException("查询到还有人入住或即将入住，不能报修房，请妥善安排当前房间的入住人员");
            }
        }
        YxRoomRepairEntity entity = new YxRoomRepairEntity();
        BeanUtil.copyProperties(dto,entity);
        entity.setCreateTime(new Date());
        entity.setCreateName(userVo.getNickName());
        entity.setTenantId(userVo.getTenantId());
        entity.setCreateId(userVo.getUserId());
        entity.setStatus(0);
        entity.setRepairType(1);
        baseMapper.insert(entity);
    }

    /**
     * 查询宿舍资产列表
     * @param dto
     * @return
     */
    @Override
    public PageVO<RoomRepairVo> pageRoomRepairList(QueryRoomRepairDto dto) {
        List<RoomRepairVo> voList=new ArrayList<>();
        UserTokenVo userVo = userUtil.getLoginUserVo();
        Page<YxRoomRepairEntity> page = new Page<>(dto.getCurrPage(),dto.getPageSize());
        QueryWrapper<YxRoomRepairEntity>  queryWrapper=new QueryWrapper();
        queryWrapper.eq("room_id",dto.getRoomId());
        queryWrapper.eq("tenant_id",userVo.getTenantId());
        queryWrapper.eq(null!=dto.getStatus(),"status",dto.getStatus());
        if(null!=dto.getPropertyType()){
            queryWrapper.exists("select 1 from yx_room_property rp where rp.property_id=yx_room_repair.property_id and rp.property_type="+dto.getPropertyType());
        }
        queryWrapper.orderByDesc("create_time");
        Page<YxRoomRepairEntity> entityPage = baseMapper.selectPage(page, queryWrapper);
        List<YxRoomRepairEntity> records = entityPage.getRecords();
        if(null!=records && !records.isEmpty()){
            voList=BeanUtil.copyToList(records,RoomRepairVo.class);
        }
        return PageUtils.get(page, voList);
    }

    @Override
    public RoomRepairVo getRoomRepairById(Long repairId) {
        RoomRepairVo vo = new RoomRepairVo();
        YxRoomRepairEntity entity = baseMapper.selectById(repairId);
        if(null!=entity){
            BeanUtil.copyProperties(entity,vo);
        }
        return vo;
    }

    /**
     * 更新宿舍报修信息
     * @param dto
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateRoomRepair(UpdateRoomRepairDto dto) {
        YxRoomRepairEntity entity = baseMapper.selectById(dto.getRepairId());
        if(null==entity){
            throw new BaseException("未查询到报修信息进行修改");
        }
        BeanUtil.copyProperties(dto,entity);
        if(2==dto.getStatus()){
            entity.setRepairType(0);
        }
        baseMapper.updateById(entity);
    }
}
